package com.neuedu.mvc.controller;

import com.neuedu.dao.EmpDao;
import com.neuedu.entity.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;
import java.util.logging.SimpleFormatter;

@WebServlet(urlPatterns = "/emp")
public class EmpController extends HttpServlet {

    public static final String LIST = "list";
    public static final String SAVE = "save";
    public static final String INSERT = "insert";
    public static final String TO_EDIT = "toEdit";
    public static final String UPDATE = "update";
    public static final String DELETE_BY_ID = "deleteById";


    private EmpDao empDao = new EmpDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //根据 条件 执行不同的功能
//        _type

        String type = request.getParameter("_type");
        if(type == null || LIST.equals(type)){
            this.queryList(request,response);
        }else if (SAVE.equals(type)){
            this.save(request,response);
        }else if (TO_EDIT.equals(type)){
            this.toEdit(request,response);
        }else if(UPDATE.equals(type)){
            this.update(request,response);
        }else if(DELETE_BY_ID.equals(type)){
            this.deleteById(request,response);
        }



    }

    private void deleteById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取主键
        String empno = request.getParameter("empno");

        int i = empDao.deleteById(Integer.parseInt(empno));


        response.sendRedirect(request.getContextPath() + "/emp?_type=list");


    }

    private void update(HttpServletRequest request, HttpServletResponse response) {
        String empno = request.getParameter("empno");
        String ename = request.getParameter("ename");
        String job = request.getParameter("job");
        String mgr = request.getParameter("mgr");
        String hiredate = request.getParameter("hiredate");
        String sal = request.getParameter("sal");
        String comm = request.getParameter("comm");
        String deptno = request.getParameter("deptno");

        System.out.println("empno = " + empno);
        System.out.println("ename = " + ename);
        System.out.println("job = " + job);
        System.out.println("mgr = " + mgr);
        System.out.println("hiredate = " + hiredate);
        System.out.println("sal = " + sal);
        System.out.println("comm = " + comm);
        System.out.println("deptno = " + deptno);

        /**
         * empno = 999
         * ename = hhhh
         * job = hhh
         * mgr = hh
         * hiredate = 2023-11-031111
         * sal = 50.6
         * comm = 60.7
         * deptno = 70.8
         */

        //2 Emp
        try {
            Emp emp = new Emp(
                    Integer.parseInt(empno),
                    ename,
                    job,
                    Integer.parseInt(mgr),
                    //2023-11-03
                    Objects.isNull(hiredate)?null:new SimpleDateFormat("yyyy-MM-dd").parse(hiredate),
                    new BigDecimal(sal),
                    new BigDecimal(comm),
                    Integer.parseInt(deptno)

            );
            //3 save
            int insert = empDao.updateById(emp);
            //4查询列表

            //浏览器  list

            //1 内部跳转
            //2 重定向
            response.sendRedirect(request.getContextPath() + "/emp?_type=list");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //查询一条记过
        String empno = request.getParameter("empno");
        Emp emp = empDao.selectOne("where empno = ?", empno);

        //共享数据
        request.setAttribute("emp",emp);

        //内部跳转  user_edit.jsp  渲染显示
        request.getRequestDispatcher("/business/emp/user_edit.jsp").forward(request,response);
    }

    /**
     * 添加员工
     * @param request
     * @param response
     */

    private void save(HttpServletRequest request, HttpServletResponse response) {
        //1 获取员工信息
        String empno = request.getParameter("empno");
        String ename = request.getParameter("ename");
        String job = request.getParameter("job");
        String mgr = request.getParameter("mgr");
        String hiredate = request.getParameter("hiredate");
        String sal = request.getParameter("sal");
        String comm = request.getParameter("comm");
        String deptno = request.getParameter("deptno");

        System.out.println("empno = " + empno);
        System.out.println("ename = " + ename);
        System.out.println("job = " + job);
        System.out.println("mgr = " + mgr);
        System.out.println("hiredate = " + hiredate);
        System.out.println("sal = " + sal);
        System.out.println("comm = " + comm);
        System.out.println("deptno = " + deptno);

        /**
         * empno = 999
         * ename = hhhh
         * job = hhh
         * mgr = hh
         * hiredate = 2023-11-031111
         * sal = 50.6
         * comm = 60.7
         * deptno = 70.8
         */

        //2 Emp
        try {
            Emp emp = new Emp(
                    Integer.parseInt(empno),
                    ename,
                    job,
                    Integer.parseInt(mgr),
                    //2023-11-03
                    Objects.isNull(hiredate)?null:new SimpleDateFormat("yyyy-MM-dd").parse(hiredate),
                    new BigDecimal(sal),
                    new BigDecimal(comm),
                    Integer.parseInt(deptno)

                    );
            //3 save
            int insert = empDao.insert(emp);
            //4查询列表

            //浏览器  list

            //1 内部跳转 (不产生新的请求)
//            request.getRequestDispatcher("/emp?_type=list").forward(request, response);
            //2 重定向 告诉浏览器重新发起请求  第二次请求
            response.sendRedirect(request.getContextPath() + "/emp?_type=list");


        } catch (ParseException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    private void queryList(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //调用
        String where = "";
        Object[] params = new Object[0];
        List<Emp> empList = empDao.selectList(where, params);


        //共享数据
        request.setAttribute("emps",empList);
        //emps 渲染 Table

        //内部跳转的 / 相对的当前项目
        request.getRequestDispatcher("/business/emp/user_list.jsp").forward(request,response);

    }


}
